﻿using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;

namespace HardcoreGame.Common.Entities
{
    public class BookcaseEntity : ModTileEntity
    {
        public const int Capacity = 15;
        public Item[] Items = new Item[15];
        public override void OnNetPlace()
        {
            for (int i = 0; i < 15; i++)
            {
                Items[i] = new Item();
            }
        }
        public override bool IsTileValidForEntity(int i, int j)
        {
            return Main.tile[i, j].TileType == 101;
        }
        public override void SaveData(TagCompound tag)
        {
            tag["items"] = Items;
        }
        public override void LoadData(TagCompound tag)
        {
            Items = tag.Get<Item[]>("items");
        }
        public override void NetSend(BinaryWriter writer)
        {
            Item[] items = Items;
            for (int i = 0; i < items.Length; i++)
            {
                ItemIO.Send(items[i], writer, true, true);
            }
        }
        public override void NetReceive(BinaryReader reader)
        {
            for (int i = 0; i < 15; i++)
            {
                Items[i] = ItemIO.Receive(reader, true, true);
            }
        }
        public static bool IsBook(Item item)
        {
            if (item.IsAir)
            {
                return true;
            }
            if (item.createTile == TileID.Books)
            {
                return true;
            }
            if (item.Name.Contains("book", System.StringComparison.InvariantCultureIgnoreCase))
            {
                return true;
            }
            if (item.type == ItemID.WaterBolt || item.type == ItemID.BookofSkulls || item.type == ItemID.DemonScythe || item.type == ItemID.CursedFlames || item.type == ItemID.GoldenShower || item.type == ItemID.CrystalStorm || item.type == ItemID.MagnetSphere || item.type == ItemID.RazorbladeTyphoon || item.type == ItemID.LunarFlareBook)
            {
                return true;
            }
            ModItem modItem = item.ModItem;
            return modItem != null && modItem.Name.Contains("book", System.StringComparison.InvariantCultureIgnoreCase);
        }
    }
}
